/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swing;

import Modele.ModeleTableau;
import Modele.Port;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

/**
 *
 * @author steven
 */
public class ListePortsInterface extends JFrame {

    private ModeleTableau modele = new ModeleTableau();
    private JTable tableau;
    private ScanneurInterface scanneurInterface;
    private JTextField numPort;
    private JComboBox typePort;

    public ListePortsInterface(ScanneurInterface si) {
        super();
        scanneurInterface = si;
        initComponents();
    }

    private void initComponents() {
        setTitle("Liste des ports à scanner");
        //setAlwaysOnTop(true);

        String[] items = new String[2];
        items[0] = "UDP";
        items[1] = "TCP";

        tableau = new JTable(modele);
        numPort = new JTextField("Numéro du port");
        typePort = new JComboBox(items);

        getContentPane().add(new JScrollPane(tableau), BorderLayout.CENTER);

        GridLayout experimentLayout = new GridLayout(2, 2);

        JPanel boutons = new JPanel();
        boutons.setLayout(experimentLayout);

        boutons.add(numPort);
        boutons.add(typePort);
        boutons.add(new JButton(new AddAction()));
        boutons.add(new JButton(new RemoveAction()));
        boutons.add(new JButton(new ScanAction()));

        getContentPane().add(boutons, BorderLayout.SOUTH);

        pack();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screenSize.width - this.getWidth()) / 2, (screenSize.height - this.getHeight()) / 2);
    }

    private class AddAction extends AbstractAction {

        private AddAction() {
            super("Ajouter");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            ajouterPort();
        }
    }

    private class RemoveAction extends AbstractAction {

        private RemoveAction() {
            super("Supprimmer");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int[] selection = tableau.getSelectedRows();

            for (int i = selection.length - 1; i >= 0; i--) {
                modele.removePort(selection[i]);
            }
        }
    }

    private class ScanAction extends AbstractAction {

        private ScanAction() {
            super("Lancer le scan");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!modele.getListPorts().isEmpty()) {
                modele.enregistrerPorts();
                scanneurInterface.lancerScanList(modele.getListPorts());
                dispose();
            }else{
                JOptionPane.showMessageDialog(null, "ERREUR: La liste des ports à scanner est vide !");
            }
        }
    }

    private void ajouterPort() {
        try {
            if (Integer.parseInt(numPort.getText()) > 0
                    && Integer.parseInt(numPort.getText()) < 65536) {
                modele.addPort(new Port(Integer.parseInt(numPort.getText()), typePort.getSelectedItem().toString()));
            } else {
                throw new NumberFormatException();
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "ERREUR: Le numéro de port doit être un nombre \n comprit entre 1 et 65536.");
        }
    }
}
